/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * A single button for interface which is used to create a physics object (either an element or a bomb)
 */

#include "GameInterfacePhysicObjectButton.h"
#include "GameInterfaceItemAndObjectContainer.h"
#include "../../Resources.h"
#include "../../Common.h"

using namespace cocos2d;

// default constructor
GameInterfacePhysicObjectButton::GameInterfacePhysicObjectButton( cocos2d::CCLayer *layer, GameInterfaceItemAndObjectContainer *master, PhysicObjectItems type, cocos2d::CCPoint buttonPos, int moneyValue )
	: GameInterfaceObject( layer ), mMaster( master ), mItemType( type ), mPosition( buttonPos ), mMoney( moneyValue ) {

	// init background sprite
	mBackgroundSprite = CCSprite::spriteWithFile( getResource( Textures::GAME_INTERFACE_BUTTON_BACKGROUND ) );
	mBackgroundSprite->setScale( RATEX * 1.5f );
	mBackgroundSprite->setPosition( mPosition );
	layer->addChild( mBackgroundSprite, Z_LAYER_INTERFACE );

	// init item sprite
	switch( mItemType ) {
	case ELEMENT_WOOD_BOX:
		{
			mItemIndicatorSprite = CCSprite::spriteWithFile( getResource( Textures::GAME_ELEMENT_WOOD ) );
			break;
		}
	case BOMB_SMALL_MINE:
		{
			mItemIndicatorSprite = CCSprite::spriteWithFile( getResource( Textures::GAME_BOMB_SMALLMINE_A ) );
			break;
		}
	case BOMB_MEDIUM:
		{
			mItemIndicatorSprite = CCSprite::spriteWithFile( getResource( Textures::GAME_BOMB_MEDIUM ) );
			break;
		}
	case ELEMENT_STEEL_BOX:
		{
			mItemIndicatorSprite = CCSprite::spriteWithFile( getResource( Textures::GAME_ELEMENT_STEEL ) );
			break;
		}
	case ELEMENT_STEEL_BAR:
		{
			mItemIndicatorSprite = CCSprite::spriteWithFile( getResource( Textures::GAME_ELEMENT_STEEL_BAR ) );
			break;
		}
	case BORDER_WOOD:
		{
			mItemIndicatorSprite = CCSprite::spriteWithFile( getResource( Textures::GAME_BORDER_WOOD_ICON ) );
			break;
		}
	}
	mItemIndicatorSprite->setScale( RATEX * 0.5f * ( 128.0f / mItemIndicatorSprite->getContentSizeInPixels().width ) );
	mItemIndicatorSprite->setPosition( mPosition );
	layer->addChild( mItemIndicatorSprite, Z_LAYER_INTERFACE + 1 );

	// create money indicator if needed
	if( moneyValue > 0 ) {
		char money[ 8 ];
		sprintf( money, "%d$", moneyValue );
		mMoneyLabel = cocos2d::CCLabelBMFont::labelWithString( money, getResource( Fonts::FONT_NORMAL ) );
		mMoneyLabel->setScaleX( RATEX * 0.35f );
		mMoneyLabel->setScaleY( RATEY * 0.35f );
		mMoneyLabel->setPosition( ccp(
			mBackgroundSprite->getPositionX() - WIN_WIDTH * 0.025f,
			mBackgroundSprite->getPositionY() - WIN_HEIGHT * 0.085f
			) );
		layer->addChild( mMoneyLabel, Z_LAYER_INTERFACE + 1 );
	}
	else mMoneyLabel = 0;
}

// default destructor
GameInterfacePhysicObjectButton::~GameInterfacePhysicObjectButton() {

}

// update function of the object
void GameInterfacePhysicObjectButton::update( cocos2d::ccTime dt ) {
	GameInterfaceObject::update( dt );
}

// function for checking whether the given coordinates are inside the background sprite (button)
bool GameInterfacePhysicObjectButton::testIfCoordinatesAreInsideBackgroundSprite( cocos2d::CCPoint screenCoords ) {
	if( std::abs( screenCoords.x - mPosition.x ) <= WIN_WIDTH * 0.1f &&
		std::abs( screenCoords.y - mPosition.y ) <= WIN_HEIGHT * 0.1f ) 
		return true;
	return false;
}

// function for initializing the item connected to this button, should return the physics object created
b2Body *GameInterfacePhysicObjectButton::createAndInitializeNewCorrespondingItem( GameCamera *camera, b2Vec2 position ) {
	return mMaster->createNewItem( camera, mItemType, position );
}

// returns the price of this object
int GameInterfacePhysicObjectButton::getObjectPrice() {
	return mMoney;
}
